package com.zh.business.modules.sys.service;

import com.zh.business.base.service.BasicServiceImplAbstract;
import com.zh.business.modules.sys.domain.SysUser;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.zh.business.modules.sys.mapper.SysUserMapper;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * Coding......
 * 2019/12/7.
 */
@Service
public class SysUserService extends BasicServiceImplAbstract<SysUser> {

    @Resource
    private SysUserMapper userMapper;


    @Override
    public Mapper<SysUser> getMapper() {
        return userMapper;
    }

    public List<SysUser> getList() {
        return userMapper.selectAll();
    }

    public Integer login(SysUser user) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken to = new UsernamePasswordToken(user.getUserName(),user.getPassword());
        try {
            subject.login(to);
        }catch (AuthenticationException e){
            e.printStackTrace();
            return -401;//账号或密码错误！
        }catch (AuthorizationException e){
            e.printStackTrace();
            return -402;//没有权限
        }
        return 1;
    }

    public SysUser getUserByUserName(String userName) {
        SysUser u = new SysUser();
        u.setUserName(userName);
        return userMapper.selectOne(u);
    }

}



